import cv2
import numpy as np
import matplotlib.pyplot as plt

# 读取图像
image = cv2.imread(r'C:\Users\26356\Desktop\flower.jpg')

# 将图像从BGR转换为RGB
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# RGB转HSV
image_hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)

# RGB转YUV
image_yuv = cv2.cvtColor(image, cv2.COLOR_RGB2YUV)

# RGB转YCbCr
image_ycbcr = cv2.cvtColor(image, cv2.COLOR_RGB2YCrCb)

# 分离通道
h, s, v = cv2.split(image_hsv)
y, u, v_yuv = cv2.split(image_yuv)
y_ycbcr, cb, cr = cv2.split(image_ycbcr)

# 显示各个通道的图像
plt.figure(figsize=(12, 8))

# HSV通道
plt.subplot(3, 3, 1)
plt.title('Hue')
plt.imshow(h, cmap='gray')
plt.axis('off')

plt.subplot(3, 3, 2)
plt.title('Saturation')
plt.imshow(s, cmap='gray')
plt.axis('off')

plt.subplot(3, 3, 3)
plt.title('Value')
plt.imshow(v, cmap='gray')
plt.axis('off')

# YUV通道
plt.subplot(3, 3, 4)
plt.title('Y (YUV)')
plt.imshow(y, cmap='gray')
plt.axis('off')

plt.subplot(3, 3, 5)
plt.title('U (YUV)')
plt.imshow(u, cmap='gray')
plt.axis('off')

plt.subplot(3, 3, 6)
plt.title('V (YUV)')
plt.imshow(v_yuv, cmap='gray')
plt.axis('off')

# YCbCr通道
plt.subplot(3, 3, 7)
plt.title('Y (YCbCr)')
plt.imshow(y_ycbcr, cmap='gray')
plt.axis('off')

plt.subplot(3, 3, 8)
plt.title('Cb (YCbCr)')
plt.imshow(cb, cmap='gray')
plt.axis('off')

plt.subplot(3, 3, 9)
plt.title('Cr (YCbCr)')
plt.imshow(cr, cmap='gray')
plt.axis('off')

plt.tight_layout()
plt.show()